var http = require('http')
var url = require('url')
var utils = require('./modules/utils')
var book = require('./services/book')
var server = http.createServer(function (req, res) {
    var urlObj = url.parse(req.url, true);
    var pathname = urlObj.pathname;
    var GET = urlObj.query;
    if (pathname == '/book') {
        switch (GET.action) {
            case 'add':
                book.add({
                    isbn: GET.isbn,
                    name: GET.name,
                    author: GET.author,
                    press: GET.press,
                    price: GET.price
                },res)
                break;
            case 'del':
                book.del(GET.id,res)
                break;
            case 'upt':
                book.upt({
                    id: GET.id,
                    isbn: GET.isbn,
                    name: GET.name,
                    author: GET.author,
                    press: GET.press,
                    price: GET.price,
                },res)
                break;
        }
    } else {
        if (pathname == '/' || pathname == '/index.html') {
                book.list(res)
        } else if (pathname == '/edit.html') {
                book.edit(GET.id,res)
        } else {
            utils.static(pathname,res)
        }
    }

});
server.listen(3000, function () {
    console.log('您的网站运行在：localhost:3000');

})